package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.MesTaskCard;
import com.ruoyi.system.service.IMesTaskCardService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 车间任务卡Controller
 * 
 * @author ruoyi
 * @date 2024-06-01
 */
@RestController
@RequestMapping("/system/card")
public class MesTaskCardController extends BaseController
{
    @Autowired
    private IMesTaskCardService mesTaskCardService;

    /**
     * 查询车间任务卡列表
     */
    @PreAuthorize("@ss.hasPermi('system:card:list')")
    @GetMapping("/list")
    public TableDataInfo list(MesTaskCard mesTaskCard)
    {
        startPage();
        List<MesTaskCard> list = mesTaskCardService.selectMesTaskCardList(mesTaskCard);
        return getDataTable(list);
    }

    /**
     * 导出车间任务卡列表
     */
    @PreAuthorize("@ss.hasPermi('system:card:export')")
    @Log(title = "车间任务卡", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesTaskCard mesTaskCard)
    {
        List<MesTaskCard> list = mesTaskCardService.selectMesTaskCardList(mesTaskCard);
        ExcelUtil<MesTaskCard> util = new ExcelUtil<MesTaskCard>(MesTaskCard.class);
        util.exportExcel(response, list, "车间任务卡数据");
    }

    /**
     * 获取车间任务卡详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:card:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(mesTaskCardService.selectMesTaskCardById(id));
    }

    /**
     * 新增车间任务卡
     */
    @PreAuthorize("@ss.hasPermi('system:card:add')")
    @Log(title = "车间任务卡", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesTaskCard mesTaskCard)
    {
//        if (!mesTaskCardService.isTaskCardIdUnique(mesTaskCard.getTaskCardId()))
//        {
//            return AjaxResult.error("任务卡编码重复");
//        }
//        return toAjax(mesTaskCardService.insertMesTaskCard(mesTaskCard));
        return toAjax(mesTaskCardService.cardOpening(mesTaskCard));

    }

    /**
     * 修改车间任务卡
     */
    @PreAuthorize("@ss.hasPermi('system:card:edit')")
    @Log(title = "车间任务卡", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesTaskCard mesTaskCard)
    {
//        if (!mesTaskCardService.isTaskCardIdUnique(mesTaskCard,false))
//        {
//            return AjaxResult.error("任务卡编码重复");
//        }
        return toAjax(mesTaskCardService.updateMesTaskCard(mesTaskCard));
    }

    /**
     * 删除车间任务卡
     */
    @PreAuthorize("@ss.hasPermi('system:card:remove')")
    @Log(title = "车间任务卡", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(mesTaskCardService.deleteMesTaskCardByIds(ids));
    }

    //车间任务拆卡
    //无法使用
    @PostMapping("/carOpening")
    public AjaxResult cardOpening(@RequestBody MesTaskCard mesTaskCard)
    {
        return toAjax(mesTaskCardService.cardOpening(mesTaskCard));
    }

    //查询任务是否完成接口
    @GetMapping("/work/is/finish")
    public AjaxResult workIsFinish(@RequestParam("taskId") String taskId) {
        boolean allCompleted = mesTaskCardService.workIsFinish(taskId);
        return AjaxResult.success(allCompleted);

    }


}
